/**
 * @class ExtGames.Deck
 * @param {Array} cards An array of cards to initialise the deck with
 * Represents the deck of all non-dealt cards
 */
ExtGames.Scorecard = function(config) {
  var config = config || {};  
  this.totals = {};
  
  ExtGames.Scorecard.superclass.constructor.call(this, config);
        
};

Ext.extend(ExtGames.Scorecard, Ext.grid.GridPanel, {
	
	 initComponent:function() {
		 
		 var fields = [];
		 fields[0] = {
			name: 'hand'
		};
		 fields[1] = { 
			name: util.getMyId(),
			type: 'int'
		 };
		 
		 var opp = util.getOpponents();
		 for (var i = 0; i < opp.length; i++) {
			 fields[i+2] = {
				name: opp[i].id,
				type: 'int'
			 };
		 }
		 
		 var data = [];
		 data[0] = [];
		 data[0][0] = 'Total';
		 data[0][1] = 0;
		 this.totals[util.getMyId()] = 0;
		 for (var i = 0; i < opp.length; i++) {
			 data[0][i+2] = 0;
			 this.totals[opp[i].id] = 0;
		 }
		 
		 var columns = [];
		 columns[0] = {
			id: 'hand',
			header: 'Hand'
		 };
		 columns[1] = {
			id: util.getMyId(),
			header: util.getName(util.getMyId())
		 };
		 for (var i = 0; i < opp.length; i++) {
			 columns[i+2] = {
				id: opp[i].id,
				header: opp[i].name
			 };
		 }
		 
		 // hard coded - cannot be changed from outside
		 var config = {
			 store: new Ext.data.SimpleStore({
				 id:0,
				 fields:fields,
				 data:data
			 }),
			 columns:columns,
			 viewConfig:{forceFit:true}
		 }; // eo config object
		  
		 // apply config
		 Ext.apply(this, Ext.apply(this.initialConfig, config));
		  
		 // call parent
		 ExtGames.Scorecard.superclass.initComponent.apply(this, arguments);
		 
	 },
	
	
	displayScores: function(newScores) {
		
		var totalRecord = this.store.getAt(this.store.getCount()-1);
		
		data = {};
		for (id in newScores) {
			data[id] = newScores[id];
			
			var total = this.totals[id];
			total += newScores[id];
			this.totals[id] = total;
			
			totalRecord.set(id, total);
		}
		
		data['hand'] = this.store.getCount();
		totalRecord.set('hand', 'Total');
		
		this.store.insert((this.store.getCount()-1),new Ext.grid.PropertyRecord(data));
				
		this.doLayout();
	},
	
	getTotals: function() {
		return this.totals;
	}
  
});